#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_hde.h>
#else
#include <api/libpub/lib_hde.h>
#endif
#include <hld/dis/vpo.h>
#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"

#ifdef CI_SUPPORT
#include <api/libci/ci_plus.h>
#include "win_ci_common.h"

static UINT16 ci_slot_str[MAX_DISP_STR_LEN];
static ID  ci_dlg_timer = OSAL_INVALID_ID;
UINT8 bDlgShow = 0;

#define WAIT_EXPIRE_TIMEOUT 2000
extern OSAL_ID control_mbf_id;
#if 0
static void cidlg_wait_msg(void);
#endif

#define CI_DLG_W	500
#define CI_DLG_H	150
#define CI_DLG_L	GET_MID_L(CI_DLG_W)
#define CI_DLG_T	GET_MID_T(CI_DLG_H)

//Clear the VSCreen
BOOL OSD_ClearVScreen(void)
{
	OSD_RECT rect;
	UINT32 vscr_idx;
	VSCR *apVscr;

	vscr_idx = osal_task_get_current_id();
	apVscr = OSD_GetTaskVscr(vscr_idx);
    if (apVscr)
    {
        apVscr->updatePending = 0;
        apVscr->lpbScr = NULL;
    }

	OSD_GetRectOnScreen(&rect);
	rect.uLeft = rect.uTop= 0;
	UINT32 trans_color = OSD_GetTransColor(g_osd_region_info.tOpenPara.eMode, FALSE);
	OSD_DrawFrame(&rect,trans_color,apVscr);
	
    return TRUE;
}

BOOL is_ci_dlg_openning(void)
{
	return bDlgShow;
}
#ifdef AUTO_UPDATE_TPINFO_SUPPORT
void set_ci_dlg_openning( BOOL show)
{
    bDlgShow = show; 
}
#endif
void win_ci_dlg_open(UINT16 *uni_str_status)
{
	UINT8 back_saved;
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_frame(CI_DLG_L, CI_DLG_T, CI_DLG_W, CI_DLG_H);
	win_compopup_set_msg_ext(NULL, (char*)uni_str_status, 0);
	win_msg_popup_open();
	bDlgShow = 1;
}

void win_ci_dlg_close()
{
#ifdef TRUE_COLOR_HD_OSD
	OBJECT_HEAD*topmenu = NULL;
	win_msg_popup_close();
	
	topmenu = menu_stack_get_top();
	if(topmenu && topmenu != (POBJECT_HEAD)&g_win_mainmenu)
	{
		OSD_ClearVScreen();
		OSD_DrawObject(topmenu, C_UPDATE_ALL);
	}
	
	bDlgShow = 0;
#else
	win_msg_popup_close();
	bDlgShow = 0;
#endif
	
}

void win_ci_dlg_show_status(UINT32 msg_code)
{
	UINT16 slot	  = msg_code >> 16;
	UINT16 ci_msg = msg_code & 0xFFFF;

	char ansi_str[MAX_DISP_STR_LEN];
	char *str;
	void *ci_buffer = (void*)win_ci_get_tmp_buf();
	UINT16 uni_str_status[MAX_DISP_STR_LEN];
	struct ci_enquiry *enquiry;
#ifdef CI_PLUS_SUPPORT
    if(IsCIPlus_Menu_Exist())
    {
        return;
    }
#endif
	sprintf(ansi_str, "Slot %d: ", slot+1);
	switch(ci_msg)
	{
		case API_MSG_CAMIN:
			strcat(ansi_str, "Initializing");
			ComAscStr2Uni((UINT8*)ansi_str, uni_str_status);
			
			if(bDlgShow == 1)
			{
				win_ci_dlg_close();
			}
			win_ci_dlg_open(uni_str_status);
			//cidlg_wait_msg();
			//win_ci_dlg_close();
			break;			
			
		case API_MSG_CAMOUT:
			strcat(ansi_str, "Uninstalling");
			ComAscStr2Uni((UINT8*)ansi_str, uni_str_status);
			
			if(bDlgShow == 1)
			{
				win_ci_dlg_close();
			}
			win_ci_dlg_open(uni_str_status);
			osal_task_sleep(200);
			//cidlg_wait_msg();
			//win_ci_dlg_close();
			break;
		case API_MSG_CTC:
		case API_MSG_DTC:			
			if(bDlgShow == 1)
			{
				win_ci_dlg_close();
			}
			break;
		case API_MSG_ENQUIRY_UPDATE:
			enquiry = api_ci_get_enquiry(ci_buffer, CI_DATA_BUFFER_LEN, slot);

			if(enquiry != NULL)
			{
				str = ci_enquiry_get_text(enquiry);
				ComAscStr2Uni(str, uni_str_status);
			}
			
			if(bDlgShow == 1)
			{
				win_ci_dlg_close();
			}
			win_ci_dlg_open(uni_str_status);
			//cidlg_wait_msg();
			//win_ci_dlg_close();	
			break;
		default:
			break;
	}

	
}


UINT8 win_ci_dlg_get_status()
{
	return bDlgShow;
	
}

#if 0
static void cidlg_wait_msg(void)
{
	OSAL_ER	retVal;
	ControlMsg_t	msg;
	UINT32	ticks,msgSize;

	ticks = osal_get_tick();
	while(1)
	{
		if(osal_msgqueue_receive(&msg,&msgSize,control_mbf_id,100) == OSAL_E_OK)
		{
			//TODO:judg if interesting with the msg
			if(msg.msgType == CTRL_MSG_SUBTYPE_STATUS_CI)
			{
				if(((msg.msgCode & 0xFFFF) == API_MSG_DTC)
					|| ((msg.msgCode & 0xFFFF) == API_MSG_CTC))
				{//stuff msg dlg here untill ci status msg got,but should resend the msg
					ap_send_msg(msg.msgType,msg.msgCode,FALSE);
					break;
				}
			}
			else if((msg.msgType == CTRL_MSG_SUBTYPE_STATUS_USBMOUNT)
				|| (msg.msgType == CTRL_MSG_SUBTYPE_STATUS_USBUNMOUNT))
			{
				ap_send_msg(msg.msgType,msg.msgCode,FALSE);
				break;
			}
		}
		else
		{
			continue;
		}

		if((osal_get_tick() - ticks) > 1000)
		{
			break;
		}
	}
}
#endif
#else // CI_SUPPORT
UINT8 win_ci_dlg_get_status()
{
    return 0;	
}
#endif


